#include "dxsprite.h"
#include "logger.h"

using namespace std;

DXSprite::DXSprite() 
{ }

DXSprite::DXSprite(GameGraphic* pGraphic, const char* nameImg, D3DCOLOR transcolor, int row, int spriteRow)
{	
    pSprite = NULL;
    numberRow = row;
    spritePerRow = spriteRow;
    position = D3DXVECTOR2(0.0f, 0.0f);

    //Create Direct3DX Sprite
    if FAILED(D3DXCreateSprite((IDirect3DDevice9*)pGraphic->GetDevice(), &pSprite))
    {	
        (Logger::GetLogger("DXSprite")).Error("Can't create sprite!");
    }
    if FAILED(D3DXGetImageInfoFromFile(nameImg, &infoImg))
    {	
        (Logger::GetLogger("DXSprite")).Error("Cant' get image info!");
    }
    if FAILED(D3DXCreateTextureFromFileEx((IDirect3DDevice9*)pGraphic->GetDevice(), 
                                nameImg,
                                infoImg.Width,
                                infoImg.Height,
                                1,
                                D3DPOOL_DEFAULT,
                                D3DFMT_UNKNOWN,
                                D3DPOOL_DEFAULT,
                                D3DX_DEFAULT,
                                D3DX_DEFAULT,
                                transcolor,
                                &infoImg,
                                NULL,
                                &pTexture))
    {	
        (Logger::GetLogger("DXSprite")).Error("Can't create texture from file!");
    }
}

int DXSprite::GetNumberSprite()
{	
    return (numberRow * spritePerRow);
}

float DXSprite::GetWidth()
{	
    return (float)(infoImg.Width / spritePerRow);
}

float DXSprite::GetHeight()
{	
    return (float)(infoImg.Height / numberRow);
}

float DXSprite::GetCoordinate_X()
{
    return position.x;
}

float DXSprite::GetCoordinate_Y()
{
    return position.y;
}

void DXSprite::SetPosition(float x, float y)
{
    position.x = x;
    position.y = y;
}
void DXSprite::Draw(int index, float scale, int transparent, float angleRotation, float x_pointRotation, float y_pointRotation)
{	
    float width = GetWidth();
    float height = GetHeight();
    //Coordinate of sprite in image
    RECT posSprite;										
    posSprite.left = (long)((index % spritePerRow) * width);
    posSprite.top = (long)((index / spritePerRow) * height);
    posSprite.right = (long)(posSprite.left + width);
    posSprite.bottom = (long)(posSprite.top + height);

    pSprite->Begin(D3DXSPRITE_ALPHABLEND);

    D3DXVECTOR2 scalling(scale, scale);
    D3DXVECTOR2 spriteCenter(x_pointRotation, y_pointRotation);
    D3DXMATRIX mTransform;
    D3DXMatrixTransformation2D(&mTransform,
                                NULL,
                                0,
                                &scalling,
                                &spriteCenter,
                                angleRotation,
                                &position);
    pSprite->SetTransform(&mTransform);
    pSprite->Draw(pTexture, &posSprite, NULL, NULL, D3DCOLOR_ARGB(transparent, 255, 255, 255));

    pSprite->End();
    return;
}

DXSprite::~DXSprite()
{	
    if (pTexture != NULL)
        pTexture->Release();
    if (pSprite != NULL)
        pSprite->Release();
}